#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <string.h>

//=========================================================================================
// 1. DATA DEFINITIONS
// Data for calendar calculations, holidays, and display names.
//=========================================================================================

// Data for Lunar Calendar Conversion (1840-2100)
// Bits 0-11: days in lunar months (1=30, 0=29). Bit 15 is month 1, Bit 14 is month 2, etc.
// Bits 12-15 of lower 16 bits: leap month (0 for none)
const unsigned int lunarInfo[] = {
    0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2, // 1840-1849
    0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977, // 1850-1859
    0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970, // 1860-1869
    0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950, // 1870-1879
    0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557, // 1880-1889
    0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0, // 1890-1899
    0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, // 1900-1909
    0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6, // 1910-1919
    0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, // 1920-1929
    0x04970,0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5, // 1930-1939
    0x092e0,0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0, // 1940-1949
    0x0cab5,0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0, // 1950-1959
    0x0a930,0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65, // 1960-1969
    0x0d530,0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520, // 1970-1979
    0x0dd45,0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20, // 1980-1989
    0x0ada0,0x14b63,0x09370,0x049f8,0x04970,0x064b0,0x168a6,0x0ea50,0x06b20,0x1a6c4, // 1990-1999
    0x0aae0,0x092e0,0x0d2e3,0x0c960,0x0d557,0x0d4a0,0x0da50,0x05d55,0x056a0,0x0a6d0, // 2000-2009
    0x055d4,0x052d0,0x0a9b8,0x0a950,0x0b4a0,0x0b6a6,0x0ad50,0x055a0,0x0aba4,0x0a5b0, // 2010-2019
    0x052b0,0x0b273,0x06930,0x07337,0x06aa0,0x0ad50,0x14b55,0x04b60,0x0a570,0x054e4, // 2020-2029
    0x0d160,0x0e968,0x0d520,0xdaa0,0x16aa6,0x056d0,0x04ae0,0x0a9d4,0x0a2d0,0x0d150, // 2030-2039
    0x0f252,0x0d520,0x0d920,0x0d6a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2, // 2040-2049
    0x0a950,0x0b557,0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052d0,0x0a9a8, // 2050-2059
    0x0e950,0x06aa0,0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950, // 2060-2069
    0x05b57,0x056a0,0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540, // 2070-2079
    0x0b5a0,0x195a6,0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46, // 2080-2089
    0x0ab60,0x09570,0x04970,0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0, // 2090-2099
    0x0ab60,0x096d5,0x092e0                                                            // 2100
};

const char *heavenlyStems[] = {"癸", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬"};
const char *earthlyBranches[] = {"亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌"};
const char *lunarMonths[] = {"", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "腊月"};
const char *lunarDays[] = {"", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
                         "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
                         "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"};
const char *solarTerms[] = {"小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满",
                          "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降",
                          "立冬", "小雪", "大雪", "冬至"};
const int solarTermCDR[] = {6, 21, 4, 19, 6, 21, 5, 20, 6, 21, 6, 22, 7, 23, 8, 23, 8, 23, 9, 24, 8, 22, 7, 22};
const char* weekdays[] = {"日", "一", "二", "三", "四", "五", "六"};

// Solar holiday data: {month, day, name}
const struct { int month; int day; const char* name; } solarHolidays[] = {
    {1, 1, "元旦节"}, {2, 14, "情人节"}, {3, 8, "妇女节"}, {3, 12, "植树节"}, {4, 1, "愚人节"},
    {4, 5, "清明节"}, {5, 1, "劳动节"}, {5, 4, "青年节"}, {6, 1, "儿童节"}, {7, 1, "建党节"},
    {8, 1, "建军节"}, {9, 10, "教师节"}, {10, 1, "国庆节"}, {12, 25, "圣诞节"}
};

// Lunar holiday data: {month, day, name}
const struct { int month; int day; const char* name; } lunarHolidays[] = {
    {1, 1, "春节"}, {1, 15, "元宵节"}, {5, 5, "端午节"}, {7, 7, "七夕节"},
    {8, 15, "中秋节"}, {9, 9, "重阳节"}, {12, 8, "腊八节"}, {12, 23, "小年"}
};


//=========================================================================================
// 2. FORWARD DECLARATIONS & TYPEDEFS
//=========================================================================================
typedef struct {
    int year, month, day;
    int lunarYear, lunarMonth, lunarDay, leapMonth; // leapMonth is a flag (1 or 0)
    int weekday;
} Date;

// Helper Functions
void flush_input_buffer();
void set_color(int foreground, int background);
void clear_screen();
void get_current_time_string(char* buffer, size_t size);
Date get_current_date();
long days_from_start(int year, int month, int day);
Date date_from_days(long days);
int is_solar_leap(int year);
void solar_to_lunar(Date *date);
Date lunar_to_solar(int l_year, int l_month, int l_day, int is_leap);
int get_solar_term(int year, int month, int day);
int get_solar_term_day(int year, int termIndex);
int get_weekday(int year, int month, int day);

// Main Feature Functions
void show_main_menu();
void query_lunar_from_solar();
void query_solar_from_lunar();
void display_month_calendar();
void days_from_today();
void date_from_offset();
void diff_between_dates();
void show_year_solar_terms();
void show_holidays();

//=========================================================================================
// 3. MAIN FUNCTION
//=========================================================================================
int main() {
    // Set console to white background and black text as a default [cite: 2]
    set_color(0, 15); // Black on White
    clear_screen();

    int choice;
    // This while(1) loop ensures the program runs until the user chooses to exit.
    while (1) {
        show_main_menu();
        printf("请输入您的选择 (1-8, 0退出): ");

        // Read user's menu choice and check if it's a valid number
        if (scanf("%d", &choice) != 1) {
            printf("\n无效输入，请输入一个数字。\n");
            flush_input_buffer(); // Clear the faulty input (e.g., letters)
            printf("\n按任意键返回主菜单...");
            getchar(); // Wait for user acknowledgment
            clear_screen();
            continue; // Skip the rest of this loop iteration
        }

        // Handle the main menu choice
        switch (choice) {
            case 1: query_lunar_from_solar(); break;
            case 2: query_solar_from_lunar(); break;
            case 3: display_month_calendar(); break;
            case 4: days_from_today(); break;
            case 5: date_from_offset(); break;
            case 6: diff_between_dates(); break;
            case 7: show_year_solar_terms(); break;
            case 8: show_holidays(); break;
            case 0: // Exit condition
                printf("\n感谢使用，再见！\n");
                set_color(7, 0); // Reset console to default colors
                return 0; // Exit the program
            default:
                printf("\n无效选择，请输入0-8之间的数字。\n");
                break;
        }

        // After a function has run, this section waits for the user to press a key
        // before clearing the screen and showing the menu again.
        printf("\n\n按任意键返回主菜单...");
        flush_input_buffer(); // IMPORTANT: Clears the newline character from the last input
        getchar();            // Now, this reliably waits for a new keypress from the user
        clear_screen();
    }

    return 0;
}


//=========================================================================================
// 4. UI AND HELPER FUNCTION IMPLEMENTATIONS
//=========================================================================================

/**
 * @brief Clears the standard input buffer.
 *
 * This function reads and discards all characters from the input buffer
 * until a newline ('\n') or the End-Of-File (EOF) is encountered. It is essential
 * for preventing `scanf` from leaving leftover characters that would cause
 * subsequent `getchar` or `scanf` calls to misbehave.
 */
void flush_input_buffer() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
}

// Sets console text and background color
void set_color(int foreground, int background) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, foreground + (background << 4));
}

// Clears the console screen
void clear_screen() {
    system("cls");
}

// Gets the current time as a formatted string [cite: 2]
void get_current_time_string(char* buffer, size_t size) {
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    snprintf(buffer, size, "%d-%02d-%02d %02d:%02d:%02d",
             tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
             tm.tm_hour, tm.tm_min, tm.tm_sec);
}

// Gets the current date as a Date struct
Date get_current_date() {
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    Date today;
    today.year = tm.tm_year + 1900;
    today.month = tm.tm_mon + 1;
    today.day = tm.tm_mday;
    return today;
}

// Checks if a solar year is a leap year
int is_solar_leap(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// Calculates total days from a reference date (0001-01-01) to the given solar date
long days_from_start(int year, int month, int day) {
    long total_days = 0;
    int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    for (int y = 1; y < year; y++) {
        total_days += is_solar_leap(y) ? 366 : 365;
    }
    
    for (int m = 1; m < month; m++) {
        total_days += days_in_month[m];
        if (m == 2 && is_solar_leap(year)) {
            total_days++;
        }
    }
    
    total_days += day;
    return total_days;
}

// Converts a total day count back to a solar date
Date date_from_days(long days) {
    Date result_date = {1, 1, 1};
    while (days > (is_solar_leap(result_date.year) ? 366 : 365)) {
        days -= is_solar_leap(result_date.year) ? 366 : 365;
        result_date.year++;
    }

    int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (is_solar_leap(result_date.year)) days_in_month[2] = 29;

    while (days > days_in_month[result_date.month]) {
        days -= days_in_month[result_date.month];
        result_date.month++;
    }
    result_date.day = (int)days;
    return result_date;
}

// Returns the number of days in a specific lunar month
int get_lunar_month_days(int year, int month) {
    if (year < 1840 || year > 2100) return 0;
    return (lunarInfo[year - 1840] & (0x8000 >> (month - 1))) ? 30 : 29;
}

// Returns the leap month for a given lunar year (0 if none)
int get_leap_month(int year) {
    if (year < 1840 || year > 2100) return 0;
    return lunarInfo[year - 1840] & 0xf;
}

// Returns the number of days in a leap month of a lunar year
int get_leap_month_days(int year) {
    if (get_leap_month(year) == 0) return 0;
    return (lunarInfo[year - 1840] & 0x10000) ? 30 : 29;
}

// Returns total days in a lunar year
int get_lunar_year_days(int year) {
    int total_days = 0;
    for (int i = 1; i <= 12; i++) {
        total_days += get_lunar_month_days(year, i);
    }
    total_days += get_leap_month_days(year);
    return total_days;
}

// Main conversion function from Solar to Lunar date
void solar_to_lunar(Date *date) {
    if (date->year < 1840 || date->year > 2100) {
        date->lunarYear = -1; // Indicate error
        return;
    }

    Date ref_solar = {1840, 1, 27}; // Ref: 1840-01-27 is Lunar 1840-01-01
    long days_diff = days_from_start(date->year, date->month, date->day) - days_from_start(ref_solar.year, ref_solar.month, ref_solar.day);

    int current_lunar_year = 1840;
    while(days_diff >= get_lunar_year_days(current_lunar_year)) {
        days_diff -= get_lunar_year_days(current_lunar_year);
        current_lunar_year++;
    }
    date->lunarYear = current_lunar_year;

    int leap_month = get_leap_month(current_lunar_year);
    date->leapMonth = 0;

    for (int m = 1; m <= 12; m++) {
        int month_days = get_lunar_month_days(current_lunar_year, m);
        if (days_diff < month_days) {
            date->lunarMonth = m;
            date->lunarDay = (int)days_diff + 1;
            return;
        }
        days_diff -= month_days;

        if (m == leap_month) {
            month_days = get_leap_month_days(current_lunar_year);
            if (days_diff < month_days) {
                date->lunarMonth = m;
                date->lunarDay = (int)days_diff + 1;
                date->leapMonth = 1; // It's a leap month
                return;
            }
            days_diff -= month_days;
        }
    }
}

// Main conversion from Lunar to Solar
Date lunar_to_solar(int l_year, int l_month, int l_day, int is_leap) {
    if (l_year < 1840 || l_year > 2100) return (Date){-1,-1,-1};

    long day_offset = 0;
    for (int y = 1840; y < l_year; y++) {
        day_offset += get_lunar_year_days(y);
    }

    int leap_month_of_year = get_leap_month(l_year);
    for (int m = 1; m < l_month; m++) {
        day_offset += get_lunar_month_days(l_year, m);
        if (m == leap_month_of_year) {
            day_offset += get_leap_month_days(l_year);
        }
    }

    if (is_leap) {
        if (l_month != leap_month_of_year) return (Date){-1,-1,-1}; // Invalid leap month
        day_offset += get_lunar_month_days(l_year, l_month);
    }
    
    day_offset += l_day - 1;

    long ref_solar_days = days_from_start(1840, 1, 27);
    return date_from_days(ref_solar_days + day_offset);
}

// Gets the day of the week (0=Sun, 1=Mon...)
int get_weekday(int year, int month, int day) {
    if (month < 3) {
        month += 12;
        year--;
    }
    int c = year / 100;
    int y = year % 100;
    int w = (y + y / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1) % 7;
    return (w + 7) % 7;
}

// Gets the day of a specific solar term (1-24) for a given year
int get_solar_term_day(int year, int termIndex) {
    double D = 0.2422;
    int C = solarTermCDR[termIndex - 1];
    int Y = year % 100;
    int L = (year - 1900) / 4;
    return (int)(Y * D + C) - L;
}

// Checks if a given date is a solar term and returns its index (1-24), or 0 otherwise
int get_solar_term(int year, int month, int day) {
    // Check first term of the month
    if (day == get_solar_term_day(year, (month - 1) * 2 + 1)) {
        return (month - 1) * 2 + 1;
    }
    // Check second term of the month
    if (day == get_solar_term_day(year, (month - 1) * 2 + 2)) {
        return (month - 1) * 2 + 2;
    }
    return 0;
}

//=========================================================================================
// 5. FEATURE FUNCTION IMPLEMENTATIONS
//=========================================================================================

void show_main_menu() {
    char time_str[50];
    get_current_time_string(time_str, sizeof(time_str));

    set_color(4, 15); // Red on White
    printf("==================== 超级万年历 ====================\n");
    printf("      当前时间: %s\n", time_str);
    set_color(1, 15); // Blue on White
    printf("====================================================\n");
    printf("    [1] 公历查询农历    |    [2] 农历查询公历\n");
    printf("    [3] 显示月历        |    [4] 查询某天距今天数\n");
    printf("    [5] 推算日期        |    [6] 计算日期差\n");
    printf("    [7] 显示二十四节气  |    [8] 显示节日\n");
    printf("----------------------------------------------------\n");
    printf("    [0] 退出程序\n");
    printf("====================================================\n");
    set_color(0, 15); // Black on White
}


void query_lunar_from_solar() {
    Date d;
    printf("请输入公历日期 (格式: YYYY MM DD): ");
    scanf("%d %d %d", &d.year, &d.month, &d.day);

    if (d.year < 1840 || d.year > 2100) {
        printf("年份超出范围 (1840-2100)！\n");
        return;
    }

    solar_to_lunar(&d);
    
    printf("\n--- 查询结果 ---\n");
    printf("公历: %d年%d月%d日\n", d.year, d.month, d.day);
    
    const char* year_stem = heavenlyStems[(d.lunarYear - 4) % 10];
    const char* year_branch = earthlyBranches[(d.lunarYear - 4) % 12];
    long day_total_diff = days_from_start(d.year, d.month, d.day) - days_from_start(1900, 1, 31);
    const char* day_stem = heavenlyStems[(day_total_diff + 10) % 10];
    const char* day_branch = earthlyBranches[(day_total_diff + 12) % 12];

    printf("农历: %s(%s)年 %s%s %s\n", year_stem, year_branch, (d.leapMonth ? "闰" : ""), lunarMonths[d.lunarMonth], lunarDays[d.lunarDay]);
    printf("干支: %s%s日\n", day_stem, day_branch);

    int term_index = get_solar_term(d.year, d.month, d.day);
    if (term_index > 0) {
        printf("节气: 今天是 %s\n", solarTerms[term_index - 1]);
    } else {
        Date temp_d = d;
        int days_to_next = 0;
        int next_term_index = 0;
        while(next_term_index == 0) {
            days_to_next++;
            long total_days = days_from_start(d.year, d.month, d.day) + days_to_next;
            Date next_date = date_from_days(total_days);
            next_term_index = get_solar_term(next_date.year, next_date.month, next_date.day);
        }
        printf("节气: 距离下一个节气 [%s] 还有 %d 天。\n", solarTerms[next_term_index-1], days_to_next);
    }
    printf("当月节气: %s(约%d日), %s(约%d日)\n",
        solarTerms[(d.month-1)*2], get_solar_term_day(d.year, (d.month-1)*2+1),
        solarTerms[(d.month-1)*2+1], get_solar_term_day(d.year, (d.month-1)*2+2));
}

void query_solar_from_lunar() {
    int ly, lm, ld, is_leap;
    printf("请输入农历年份 (1840-2100): ");
    scanf("%d", &ly);
    printf("请输入农历月份: ");
    scanf("%d", &lm);
    printf("请输入农历日: ");
    scanf("%d", &ld);
    printf("该月是否是闰月? (1=是, 0=否): ");
    scanf("%d", &is_leap);

    if (ly < 1840 || ly > 2100) {
        printf("年份超出范围 (1840-2100)！\n");
        return;
    }
    
    Date result = lunar_to_solar(ly, lm, ld, is_leap);
    if (result.year == -1) {
        printf("输入无效的农历日期！可能是该年没有这个闰月。\n");
    } else {
        printf("\n--- 查询结果 ---\n");
        printf("农历 %d年 %s%s %s 对应的公历日期是: %d年%d月%d日\n",
               ly, (is_leap ? "闰" : ""), lunarMonths[lm], lunarDays[ld],
               result.year, result.month, result.day);
    }
}

void display_month_calendar() {
    int y, m;
    printf("请输入要查询的公历年月 (格式: YYYY MM): ");
    scanf("%d %d", &y, &m);

    if (y < 1840 || y > 2100) {
        printf("年份超出范围 (1840-2100)！\n");
        return;
    }
    
    printf("\n             %d年 %d月\n", y, m);
    printf("===================================================\n");
    set_color(4, 15); printf("  日  "); set_color(0, 15);
    printf("   一     二     三     四     五   ");
    set_color(4, 15); printf("  六\n"); set_color(0, 15);
    printf("===================================================\n");

    int first_day_weekday = get_weekday(y, m, 1);
    for (int i = 0; i < first_day_weekday; i++) {
        printf("       ");
    }

    int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (is_solar_leap(y)) days_in_month[2] = 29;

    for (int d = 1; d <= days_in_month[m]; d++) {
        int current_weekday = get_weekday(y, m, d);
        if (current_weekday == 0 || current_weekday == 6) set_color(4, 15);

        printf("%3d", d);
        
        Date temp_date = {y, m, d};
        solar_to_lunar(&temp_date);
        int term = get_solar_term(y, m, d);
        
        set_color(2, 15); // Green for lunar info
        if(term > 0) {
            printf(" %-4s", solarTerms[term-1]);
        } else {
            printf(" %-4s", lunarDays[temp_date.lunarDay]);
        }
        set_color(0, 15);

        if (current_weekday == 6) {
             printf("\n");
        }
    }
    printf("\n");
}


void days_from_today() {
    Date target, today = get_current_date();
    printf("请输入一个目标公历日期 (格式: YYYY MM DD): ");
    scanf("%d %d %d", &target.year, &target.month, &target.day);
    
    long target_days = days_from_start(target.year, target.month, target.day);
    long today_days = days_from_start(today.year, today.month, today.day);
    
    long diff = target_days - today_days;
    
    printf("\n--- 查询结果 ---\n");
    if (diff > 0) {
        printf("%d年%d月%d日 比 今天 (%d-%d-%d) 晚 %ld 天\n",
               target.year, target.month, target.day,
               today.year, today.month, today.day, diff);
    } else if (diff < 0) {
        printf("%d年%d月%d日 比 今天 (%d-%d-%d) 早 %ld 天\n",
               target.year, target.month, target.day,
               today.year, today.month, today.day, -diff);
    } else {
        printf("您输入的日期就是今天！\n");
    }
}

void date_from_offset() {
    int offset;
    Date today = get_current_date();
    printf("请输入一个天数: ");
    scanf("%d", &offset);

    long today_days = days_from_start(today.year, today.month, today.day);

    Date future_date = date_from_days(today_days + offset);
    Date past_date = date_from_days(today_days - offset);
    
    printf("\n--- 查询结果 ---\n");
    printf("距离今天 (%d-%d-%d) 【%d】 天后的日期是: %d年%d月%d日\n",
           today.year, today.month, today.day, offset,
           future_date.year, future_date.month, future_date.day);
    printf("距离今天 (%d-%d-%d) 【%d】 天前的日期是: %d年%d月%d日\n",
           today.year, today.month, today.day, offset,
           past_date.year, past_date.month, past_date.day);
}

void diff_between_dates() {
    Date d1, d2;
    printf("请输入第一个公历日期 (格式: YYYY MM DD): ");
    scanf("%d %d %d", &d1.year, &d1.month, &d1.day);
    printf("请输入第二个公历日期 (格式: YYYY MM DD): ");
    scanf("%d %d %d", &d2.year, &d2.month, &d2.day);
    
    long days1 = days_from_start(d1.year, d1.month, d1.day);
    long days2 = days_from_start(d2.year, d2.month, d2.day);
    
    long diff = days1 > days2 ? days1 - days2 : days2 - days1;
    
    printf("\n--- 查询结果 ---\n");
    printf("两个日期之间相差 %ld 天。\n", diff);
}

void show_year_solar_terms() {
    int year;
    printf("请输入要查询的年份 (1900-2100): ");
    scanf("%d", &year);

    if (year < 1900 || year > 2100) {
         printf("年份超出建议范围 (1900-2100)！\n");
        return;
    }
    
    printf("\n--- %d年 二十四节气 ---\n", year);
    for (int i = 0; i < 24; i++) {
        int m = i / 2 + 1;
        int d = get_solar_term_day(year, i + 1);
        printf("%-10s: %2d月%2d日\t", solarTerms[i], m, d);
        if ((i + 1) % 4 == 0) {
            printf("\n");
        }
    }
    printf("\n");
}

void show_holidays() {
    int choice;
    printf("请选择要查询的节日类型:\n");
    printf("  [1] 公历节日\n");
    printf("  [2] 农历节日\n");
    printf("您的选择: ");
    scanf("%d", &choice);
    
    if (choice == 1) {
        int month;
        printf("请输入要查询公历节日的月份 (1-12): ");
        scanf("%d", &month);
        printf("\n--- 公历 %d月 的节日 ---\n", month);
        int count = 0;
        for (size_t i = 0; i < sizeof(solarHolidays) / sizeof(solarHolidays[0]); i++) {
            if (solarHolidays[i].month == month) {
                printf("%s (%d月%d日)\n", solarHolidays[i].name, solarHolidays[i].month, solarHolidays[i].day);
                count++;
            }
        }
        if (count == 0) {
            printf("该月没有公历节日记录。\n");
        }
    } else if (choice == 2) {
        printf("\n--- 主要的农历节日 ---\n");
        for (size_t i = 0; i < sizeof(lunarHolidays) / sizeof(lunarHolidays[0]); i++) {
            printf("%s (%s%s)\n", lunarHolidays[i].name, lunarMonths[lunarHolidays[i].month], lunarDays[lunarHolidays[i].day]);
        }
    } else {
        printf("无效选择！\n");
    }
}